<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @submit.native.prevent @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input placeholder="请验编号" v-model="dataForm.queryCheckId" :clearable="true" type="number" size="mini">
          <template slot="prepend">请验编号</template>
        </el-input>
      </el-form-item>
      <br />
      <el-form-item>
        <el-input placeholder="品名" v-model="dataForm.productName" :clearable="true" size="mini">
          <template slot="prepend">品名</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-input placeholder="请验人" v-model="dataForm.registCheckUserName" :clearable="true" size="mini">
          <template slot="prepend">请验人</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-input placeholder="GMP编码" v-model="dataForm.productGmp" :clearable="true" size="mini">
          <template slot="prepend">编码</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-input placeholder="物料编码" v-model="dataForm.batchNum" :clearable="true" size="mini">
          <template slot="prepend">物料编码</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-select v-model="dataForm.status" clearable placeholder="全部状态" size="mini">
          <el-option label="待处理" value="0" />
          <el-option label="待取样" value="1" />
          <el-option label="待收样" value="2" />
          <el-option label="待分发" value="3" />
          <el-option label="检验中" value="4,5" />
          <el-option label="待结论" value="6" />
          <el-option label="待放行" value="7,10" />
          <el-option label="已完成" value="8,11" />
        </el-select>
      </el-form-item>

      <el-form-item v-if="types.length != 0">
        <el-select v-model="dataForm.checkTypeId" clearable placeholder="全部分类" size="mini">
          <el-option v-for="t in types" :key="t.checkTypeId" :label="t.checkTypeName" :value="t.checkTypeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" class="pan-btn primary-btn" @click="searchData()" icon="el-icon-search" size="mini">搜索</el-button>
      </el-form-item>

      <el-form-item v-if="isAuth('check:regist')">
       <!-- <dropdown-menu :items="types" title="发起" @getSelect="getSelectType" /> -->

        <el-dropdown @command="getSelectType">
          <el-button type="success" size="mini">
            发起<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="(item,index) in types" :command="item" :key="item.checkTypeId">{{item.title}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </el-form-item>
    </el-form>

    <el-table :data="dataList" v-loading="dataListLoading" border fit highlight-current-row style="width: 100%">
      <el-table-column prop="checkId" header-align="center" align="center" label="编号" width="80">
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态" width="100">
        <template slot-scope="{row}">
          <el-tag :type="statusFilter(row.status,'type')" size="small">
            {{ statusFilter(row.status,'name') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="checkTypeName" header-align="center" align="center" label="分类" width="100">
      </el-table-column>
      <el-table-column prop="productName" header-align="center" align="center" label="品名">
      </el-table-column>
      <el-table-column prop="batchNum" header-align="center" align="center" label="物料编码">
      </el-table-column>
      <el-table-column prop="productSpec" header-align="center" align="center" label="货品规格">
        <template slot-scope="{row}">
          <span v-html="row.productSpec"></span>
        </template>
      </el-table-column>
      <el-table-column prop="productPackSpec" header-align="center" align="center" label="包装规格">
      </el-table-column>
      <el-table-column prop="procedureName" header-align="center" align="center" label="工序">
        <template slot-scope="{row}">
          {{ row.procedureName }}
        </template>
      </el-table-column>
      <el-table-column prop="registCheckDepartmentName" header-align="center" align="center" label="请验部门" width="100">
      </el-table-column>
      <el-table-column prop="registCheckUserName" header-align="center" align="center" label="请验人" width="100">
      </el-table-column>
      <el-table-column prop="registCheckDate" header-align="center" align="center" label="请验时间" width="100">
        <template slot-scope="{row}">{{row.registCheckDate | changeTime}}</template>
      </el-table-column>
      <el-table-column v-if="dataForm.checkTypeId == 6" prop="stabilityModeName" header-align="center" align="center"
        label="考察方式">
        <template slot-scope="{row}">
          {{ row.stabilityModeName + ' ' + row.timeValue + row.timeName }}
        </template>
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注">
      </el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="success" class="pan-btn success-btn" size="mini" v-if="scope.row.status > 0" @click="print(scope.row,'请验单')">打印请验单</el-button></br>
          <el-button type="primary" plain size="mini" v-if="scope.row.status > 0" @click="print(scope.row,'货位卡')">打印货位卡</el-button></br>
          <el-button type="primary" plain size="mini" v-if="scope.row.status > 0 && yfb(scope.row.checkTypeName)" @click="print(scope.row,'物料验收单')">打印物料验收单</el-button></br>
          <el-button type="info" plain size="mini" v-if="scope.row.status > 0 && yfb(scope.row.checkTypeName)" @click="print(scope.row,'取样记录')">打印取样记录</el-button></br>
          <el-button type="danger" class="pan-btn danger-btn" size="mini" v-if="scope.row.status == 0 && scope.row.checkReturn.toUserId == userInfo.userId"
            @click="handle(scope.row)">处理</el-button>
          <el-button type="primary" class="pan-btn primary-btn" size="mini" v-if="scope.row.status == 1 && scope.row.registCheckUserId == userInfo.userId"
            @click="update(scope.row)">修改</el-button>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="详情" width="100">
        <template slot-scope="scope">
          <el-button type="info" class="pan-btn info-btn" size="mini" @click="info(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>

    <regist-check v-if="registVisible" @close="close('registVisible')" />

    <check-info v-if="infoVisible" @close="close('infoVisible')" @refreshData="getDataList" :childDataForm="childDataForm" />

    <select-version v-if="selectVersionVisible" @close="close('selectVersionVisible')" @getSelectVersion="getSelectVersion"
      :childDataForm="childDataForm" />

    <print v-if="printVisible" @close="close('printVisible')" :childDataForm="childDataForm" />

  </div>
</template>

<script>
  import {
    mapGetters
  } from 'vuex'
  import CommonMethod from '@/views/mixin/common'
  import {
    checkList,
    getCheckTypes,
    getVersion
  } from '@/api/check'

  import {
    CODE_OK
  } from '@/common/js/config'
  import {
    isBlank
  } from '@/common/js/utils'
  import CheckInfo from '@/views/modules/process/common/check-info'
  import DropdownMenu from '@/components/Share/DropdownMenu'
  import SelectVersion from '@/views/modules/common/select-version'
  import Print from '@/views/modules/printmodel/printchoice'
  export default {
    name: 'qingyan',
    mixins: [CommonMethod],
    components: {
      CheckInfo,
      DropdownMenu,
      SelectVersion,
      Print
    },
    data() {
      return {
        historyButton:false,
        currentStatus: 0,
        printClassify: '请验单',
        registVisible: false,
        infoVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        childDataForm: '',
        types: [],
        selectVersionVisible: false,
        printVisible: false
      }
    },
    mounted() {
      this.getInfo()
    },
    computed: {
      ...mapGetters([
        'userInfo'
      ])
    },
    methods: {
      yfb(checkTypeName){
        if("原料" == checkTypeName|| "辅料" == checkTypeName|| "包材" == checkTypeName|| "其他" == checkTypeName){
          return true;
        }else{
          return false;
        }
      },
      getSelectVersion(v) {
        this.$router.push(this.getPrintCheckUrl(v.checkTypeId, v.versionNum))
      },
      getSelectType(type) {
        if (this.wk(type.checkTypeId)) {
          this.$router.push({
            name: '稳考室'
          })
        } else if (this.env(type.checkTypeId)) {
          this.$router.push({
            name: '环境监测',
            params: {
              opt: 'showSelect'
            }
          })
        }else if(type.checkTypeId == 10){
          this.$router.push("/registcheck10-01");
        } else {
          const params = {
            checkTypeId: type.checkTypeId,
            printClassify: this.printClassify
          }
          getVersion(params).then(res => {
            if (res.code === CODE_OK) {
              this.getSelectVersion(res.list.find((l) => (l.isCurrent == 0)))
              //只有原辅包才选择版本
              // if (this.yfb(type.checkTypeId)) {
              //   this.childDataForm = res.list
              //   this.selectVersionVisible = true
              // }else{
              //   this.getSelectVersion(res.list.find((l) => (l.isCurrent == 0)))
              // }
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      },
      print(row,tag) {
        this.childDataForm = JSON.parse(JSON.stringify(row))
        this.$set(this.childDataForm, 'printClassify', this.printClassify)
        this.$set(this.childDataForm, 'printType', tag)
        this.printVisible = true
      },
      searchData() {
        this.pageIndex = 1
        this.getDataList()
      },
      getInfo() {
        this.types = this.userInfo.types.map(t => {
          this.$set(t, 'title', t.checkTypeName)
          return t
        });
        this.getDataList()
      },
      getDataList() {
        this.dataListLoading = true
        const params = {
          page: this.pageIndex,
          limit: this.pageSize,
          status: this.dataForm.status || '0,1,2,3,4,5,6,10,7,8,11',
          checkTypeId: this.dataForm.checkTypeId || this.types.map(t => {
            return t.checkTypeId
          }).join(','),
          productName: this.dataForm.productName || null,
          registCheckUserName: this.dataForm.registCheckUserName || null,
          productGmp: this.dataForm.productGmp || null,
          batchNum: this.dataForm.batchNum || null,
          checkId: this.dataForm.queryCheckId || null
        }
        checkList(params).then(res => {
          if (res.code === CODE_OK) {
            this.dataList = res.page.list
            this.totalPage = res.page.totalCount
          } else {
            this.$message.error(res.msg)
          }
          this.dataListLoading = false
        }).catch()
      },
      handle(row) {
        let returnInfo = '退回理由：' + row.checkReturn.returnReason + '</br>' + row.checkReturn.fromUserName + '在 ' + this.statusMap(
          row.checkReturn.fromStatus) + ' 时退回了您的请验单  ' + '</br>' + row.checkReturn.returnTime
        this.$confirm(returnInfo, '提示', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '去处理',
          cancelButtonText: '再想想',
          type: 'warning'
        }).then(() => {
          this.$router.push({
            path: this.getPrintCheckUrl(row.checkTypeId, row.printCheckVersion),
            query: {
              checkId: row.checkId,
              fromUserName: row.checkReturn.fromUserName,
              fromStatus: row.checkReturn.fromStatus,
              checkReturnId: row.checkReturn.checkReturnId
            }
          })
        }).catch(() => {})
      },
      close(val) {
        this[val] = !this[val]
      },
      update(row) {
        this.$router.push({
          path: this.getPrintCheckUrl(row.checkTypeId, row.printCheckVersion),
          query: {
            checkId: row.checkId,
            checkReturnId: 0
          }
        })
      },
      add() {
        this.registVisible = !this.registVisible
      },
      info(row) {
        this.childDataForm = {
          currentStatus: this.currentStatus,
          info: row
        }
        this.infoVisible = !this.infoVisible
      },
      statusMap(status) {
        const map = {
          0: '请验',
          1: '取样',
          2: '收样',
          3: '分样',
          4: '检验',
          5: '检验',
          6: '结论',
          10: '放行',
          7: '放行'
        }
        return map[status]
      },
      statusFilter(status, val) {
        const statusMap = {
          0: {
            name: '待处理',
            type: 'danger'
          },
          1: {
            name: '待取样',
            type: 'primary'
          },
          2: {
            name: '待收样',
            type: 'warning'
          },
          3: {
            name: '待分发',
            type: 'warning'
          },
          4: {
            name: '检验中',
            type: 'warning'
          },
          5: {
            name: '检验中',
            type: 'warning'
          },
          6: {
            name: '待结论',
            type: 'warning'
          },
          10: {
            name: '待放行',
            type: 'success'
          },
          7: {
            name: '待放行',
            type: 'success'
          },
          8: {
            name: '待确认',
            type: 'success'
          },
          11: {
            name: '已完成',
            type: 'success'
          }
        }
        return statusMap[status][val]
      }
    }
  }
</script>

<style scoped>
  >>>.el-form-item__content {
    line-height: normal;
  }
</style>
